package com.example.demo.util;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.DTO.ResultDTO;
import com.example.demo.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.time.LocalDateTime;
import java.util.Date;


/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/17 14:05
 */
@Component("TokenUtils")
public class TokenUtils {
    //private static final String SECRET = "cn.niter.sso";
    private static String SECRET ;
    @Value("${site.jwt.secret}")
    public  void setSecret(String SECRET) {
        this.SECRET= SECRET;
    }
    private  TokenUtils tokenUtils;

    public String getToken(User user) {
        String token = "";
        token = JWT.create()
                .withClaim("name", user.getName())
                .withClaim("id",user.getId())
                .withClaim("email",user.getEmail())
                .withClaim("time",System.currentTimeMillis())
                .withIssuer("hik")
                .withExpiresAt(new Date(System.currentTimeMillis() + 3600000 * 24 * 1))//一天
                .sign(Algorithm.HMAC256(SECRET));// 以 password 作为 token 的密钥
        return token;
    }
    public static ResultDTO verifyToken(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        try{
            DecodedJWT verify = jwtVerifier.verify(token);
            User user = new User();
            user.setId(verify.getClaim("id").asInt());
            user.setName(verify.getClaim("name").asString());
            user.setEmail(verify.getClaim("email").asString());
            return ResultDTO.okOf(user);
        }catch (JWTVerificationException e) {
            //System.err.println("token验证失败："+e.getMessage());
            return ResultDTO.errorOf("token验证失败："+e.getMessage());
        }
    }
    public static void setTokenWhite(String token,Integer id) {
        if(!token.isEmpty()&& StringUtils.isNotEmpty(token)){
            RedisUtil.setWhite(token,id);
        }

    }
    public static boolean isTokenWhite(String token) {
        if(!token.isEmpty()&&StringUtils.isNotEmpty(token)){
            return RedisUtil.keyIsExists(token);
        }
        return false;
    }
    public static void deleteTokenWhite(String token){
        if(!token.isEmpty()){
            RedisUtil.deleteWhite(token);
        }
    }
    public static void setTokenBlack(String token){
        if(!token.isEmpty()){
            RedisUtil.hset("blackList",token,Long.toString(System.currentTimeMillis()));
        }
    }
    public static Boolean isTokenBlack(String token){
        if(!token.isEmpty()){
            return RedisUtil.hasKey("blackList",token);
        }
        return false;
    }

}
